package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Cart;
import com.ruoyi.system.domain.OrderItem;
import com.ruoyi.system.service.ICartService;
import com.ruoyi.system.service.IOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-03
 */
@Service
public class OrdersServiceImpl implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ICartService cartService;
    @Autowired
    private IOrderItemService itemService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单列表
     *
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders) {
        return ordersMapper.selectOrdersList(orders);
    }

    @Override
    public Orders selectOrders(Orders orders) {
        return ordersMapper.selectOrders(orders);
    }

    /**
     * 新增订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        orders.setCreateTime(DateUtils.getNowDate());
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }
    @Override
    public int delete(Orders orders) {
        return ordersMapper.delete(orders);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }

    @Override
    public Long settle(Long[] ids) {
        Long userId = SecurityUtils.getUserId();
        List<Cart> carts = Arrays.stream(ids).map(id -> cartService.selectCartById(id)).collect(Collectors.toList());
        double totalPrice = carts.stream().mapToDouble(Cart::getSum).sum();
        String orderNo = String.valueOf(new Date().getTime());
        Orders orders = new Orders(null, orderNo, userId,null,totalPrice,null,null);
        int i = insertOrders(orders);
        Long id = orders.getId();
        if (i >0) {
            carts.forEach(cart -> {
                OrderItem item = new OrderItem();
                item.setOrderId(id);
                item.setProductId(cart.getProductId());
                item.setUnitPrice(cart.getPrice());
                item.setNum(cart.getNum());
                itemService.insertOrderItem(item);
            });
        }
        cartService.deleteCartByIds(ids);
        return id;
    }
}
